package com.huang.consumer_mall.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.huang.consumer_mall.model.Customer;

import java.util.Calendar;

public class JwtUtils {

    /**
     * 生成token header payload sing
     */

    private static final String sign = "wjhdf0erfrrfh88e2354$%^";
    public static String getToken (Customer customer){

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,1);

        //创建JWT builder
        JWTCreator.Builder builder = JWT.create();

        //payload
        builder.withClaim("cusId",customer.getCusId());
        builder.withClaim("cusAccount",customer.getCusAccount());
        builder.withClaim("cusName",customer.getCusName());
        builder.withClaim("flag",customer.getFlag());

       String token = builder.withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(sign));

        return token;
    }
    /**
     * 验证token
     */
    public static DecodedJWT verify(String token){
      return   JWT.require(Algorithm.HMAC256(sign)).build().verify(token);
    }

    /**
     * 获取token信息方法
     */
    /*public static DecodedJWT getTokenInfo(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(sign)).build().verify(token);
        return verify;
    }*/
}
